---
title: "Integrate memory in pipe"
description: "Learn how to integrate memory in a pipe using BaseAI."
tags:
    - baseai
    - langbase
    - learn
    - memory
section: "nodejs"
published: 2024-09-24
modified: 2024-09-24
---

# Integrate memory in pipe

### Learn how to integrate memory in a pipe using BaseAI

<Note sub="/learn">
This guide is part of the /learn BaseAI course. For context, [start from the beginning](/learn) to follow along.
</Note>

---

In this learn guide, you will integrate `chat-with-docs` [memory](/learn/nodejs/create-memory) in the summarizer [pipe](/learn/nodejs/configure-pipe) using BaseAI.

---

## Integrate memory in pipe

Open the pipe file present at `/baseai/pipes/summarizer.ts`, import the memory at the top and call it as a function in the `memory` array.

<CodeGroup exampleTitle="Chat with docs RAG" title="Integrate memory with the pipe">

```ts {{ title: './baseai/pipes/summarizer.ts' }}
import { PipeI } from '@baseai/core';
import getCurrentWeatherTool from '../tools/get-current-weather';
import chatWithDocsMemory from '../memory/chat-with-docs';

const pipeSummarizer = (): PipeI => ({
	apiKey: process.env.LANGBASE_API_KEY!, // Replace with your API key https://langbase.com/docs/api-reference/api-keys
	name: 'summarizer',
	description: 'A pipe that summarizes content and make it less wordy',
	status: 'public',
	model: 'openai:gpt-4o-mini',
	stream: true,
	json: false,
	store: true,
	moderate: true,
	top_p: 1,
	max_tokens: 1000,
	temperature: 0.7,
	presence_penalty: 1,
	frequency_penalty: 1,
	stop: [],
	tool_choice: 'auto',
	parallel_tool_calls: false,
	messages: [
		{ role: 'system', content: `You are a content summarizer. You will summarize content without loosing context into less wordy to the point version.` },
		{ role: 'system', name: 'rag', content: "Below is some CONTEXT for you to answer the questions. ONLY answer from the CONTEXT. CONTEXT consists of multiple information chunks. Each chunk has a source mentioned at the end.\n\nFor each piece of response you provide, cite the source in brackets like so: [1].\n\nAt the end of the answer, always list each source with its corresponding number and provide the document name. like so [1] Filename.doc.\n\nIf you don't know the answer, just say that you don't know. Ask for more context and better questions if needed." }
	],
	variables: [],
    memory: [chatWithDocsMemory()],
    tools: [getCurrentWeatherTool()]
});

export default pipeSummarizer;
```

```ts {{ title: 'index.ts' }}
import 'dotenv/config';
import {Pipe} from '@baseai/core';
import pipeSummarizer from './baseai/pipes/summarizer';

const pipe = new Pipe(pipeSummarizer());

const userMsg = `What's the weather in San Francisco?`;

async function main() {
	const response = await pipe.run({
		messages: [{role: 'user', content: userMsg}],
		stream: false,
	});

	console.log(response.completion);
}

main();
```

```ts {{ title: './baseai/tools/get-current-weather.ts' }}
import {ToolI} from '@baseai/core';

export async function getCurrentWeather(location: string, unit: string) {
	return `Weather in ${location} is 72 degrees ${unit === 'celsius' ? 'Celsius' : 'Fahrenheit'}`;
}

const getCurrentWeatherTool = (): ToolI => ({
	run: getCurrentWeather,
	type: 'function' as const,
	function: {
		name: 'getCurrentWeather',
		description: 'Get the current weather for a given location',
		parameters: {
			type: 'object',
			properties: {
				location: {
					type: 'string',
					description: 'The city and state, e.g. San Francisco, CA',
				},
				unit: {
					type: 'string',
					enum: ['celsius', 'fahrenheit'],
				},
			},
			required: ['location'],
		},
	},
});

export default getCurrentWeatherTool;
```
</CodeGroup>

Note that we have included a RAG prompt to provide context for the answers.

---

_In the next learn guide, we will run the pipe with the memory and see how it works._

---
